<template>
  <div class="register-container">
    <div class="register-form-container">
      <div class="register-title">
        <h2>注册账号</h2>
      </div>
      
      <el-form ref="registerForm" :model="registerData" :rules="rules" label-width="80px" class="register-form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerData.username" placeholder="请输入用户名" clearable />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="registerData.password" 
            type="password" 
            placeholder="请输入密码" 
            show-password 
            clearable 
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input 
            v-model="registerData.confirmPassword" 
            type="password" 
            placeholder="请确认密码" 
            show-password 
            clearable 
          />
        </el-form-item>
        
        <el-form-item label="姓名" prop="name">
          <el-input v-model="registerData.name" placeholder="请输入姓名" clearable />
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerData.email" placeholder="请输入邮箱" clearable />
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="registerData.phone" placeholder="请输入手机号" clearable />
        </el-form-item>
        
        <el-form-item label="部门" prop="department">
          <el-input v-model="registerData.department" placeholder="请输入部门" clearable />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRegister">注册</el-button>
          <el-button @click="goToLogin">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/store/auth'
import { formRules } from '@/utils/validate'

export default {
  name: 'Register',
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    const registerForm = ref(null)
    const loading = ref(false)
    
    const registerData = reactive({
      username: '',
      password: '',
      confirmPassword: '',
      name: '',
      email: '',
      phone: '',
      department: ''
    })
    
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== registerData.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    
    const rules = {
      username: [
        formRules.required('请输入用户名'),
        { min: 3, max: 20, message: '用户名长度在3到20个字符之间', trigger: 'blur' }
      ],
      password: [
        formRules.required('请输入密码'),
        formRules.password()
      ],
      confirmPassword: [
        formRules.required('请确认密码'),
        { validator: validateConfirmPassword, trigger: 'blur' }
      ],
      name: [
        formRules.required('请输入姓名')
      ],
      email: [
        formRules.required('请输入邮箱'),
        formRules.email()
      ],
      phone: [
        formRules.phone('请输入有效的手机号码')
      ],
      department: [
        formRules.required('请输入部门')
      ]
    }
    
    const handleRegister = () => {
      registerForm.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            // 去掉确认密码字段
            const { confirmPassword, ...registerPayload } = registerData
            await authStore.register(registerPayload)
            ElMessage.success('注册成功，请登录')
            router.push('/login')
          } catch (error) {
            console.error('注册失败', error)
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    const goToLogin = () => {
      router.push('/login')
    }
    
    return {
      registerForm,
      registerData,
      rules,
      loading,
      handleRegister,
      goToLogin
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  width: 100vw;
  background-color: #f5f7fa;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-form-container {
  width: 500px;
  padding: 30px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
}

.register-title h2 {
  font-size: 24px;
  color: #409EFF;
}
</style> 